######################################################################
#create by Michael Zhao 2012.06.12
######################################################################

# Global Defines
ROOT_DIR = $(shell (dirname `pwd`))
DVMI_DIR = $(ROOT_DIR)/dvmi
ROOTFS_DIR = `pwd`
INSTALL_DIR = $(ROOT_DIR)/install
MAPI_DIR = $(ROOT_DIR)/mapi
BSEAV =${REFSW}/BSEAV

APPNAME 	= dvmi.elf
#LIBNAME     = libdvmi.so



DVMI_DEBUG:=y


# Target
TARGETS = $(ROOTFS_DIR)/$(APPNAME)
#TARGETS = $(ROOTFS_DIR)/$(LIBNAME)
default:$(TARGETS)

# SRCS
SRC_DIR := $(DVMI_DIR)/src
OBJ_DIR := $(DVMI_DIR)/objs

#SRCS := $(wildcard $(SRC_DIR)/*.c)
#OBJS := $(addprefix $(OBJ_DIR)/, $(notdir $(patsubst %.c, %.o, $(SRCS))))

DVMI_SRC:= $(wildcard $(SRC_DIR)/*.c)
DVMI_OBJ=$(patsubst %.c,%.o,$(DVMI_SRC))
DVMI_NOT_USE_OBJ+= 	\
			$(SRC_DIR)/dvmi_eprom.o	\
			$(SRC_DIR)/dvmi_vod.o	\
			$(SRC_DIR)/mi_eprom.o	\
			$(SRC_DIR)/mi_pvr.o 	\
			$(SRC_DIR)/mi_os.o 	\
			$(SRC_DIR)/mi_rtos.o	\
			
			
OBJS:=$(addprefix $(OBJ_DIR)/,$(notdir $(filter-out  $(DVMI_NOT_USE_OBJ),$(DVMI_OBJ))))



BUILD_DATE:=$(shell date)

# Tools
LD      := mipsel-linux-ld
CC      := mipsel-linux-gcc
CXX     := mipsel-linux-g++
AR      := mipsel-linux-ar
STRIP   := mipsel-linux-strip
NM      := mipsel-linux-nm
CP		:= cp
MV		:= mv


# Defines
#DEFINES = -DCONFIG_MOD_INPUT_GEHUA -DBUILD_TIME="\"$(BUILD_DATE)\""
#DEFINES = -DCONFIG_MOD_INPUT_ZG_RC_001 -DBUILD_TIME="\"$(BUILD_DATE)\""

DEFINES += \
	-DBCHP_VER=BCHP_VER_B0                 \
	-DBMEM_REENTRANT_CONFIG=BMEM_REENTRANT \
	-DHAVE_KEDA_TTS                        \
	-DHAVE_KEDA_TTS                        \
	-DHTSW_HAS_BCM_FRONTPANEL=0            \
	-DHTSW_HAS_VFD_FRONTPANEL=0            \
	-DHTSW_HAS_REAL_STANDBY_FRONTPANEL=1   \
	-DHTSW_HAS_BCM_FRONTEND=1              \
	-DHTSW_HAS_0297E_FRONTEND=0            \
	-DHTSW_SYSTEM_COMMAND_PROCESS					 \


# Includes
INCLUDEDIRS = -I. \
	-I$(DVMI_DIR)/inc \
	-I$(DVMI_DIR)/inc/dvmi \
	-I$(DVMI_DIR)/src \
	-I$(DVMI_DIR)/libs/include \
	-I$(MAPI_DIR)/include \
	#-I$(BSEAV)/lib/bgfx/include \
	#-I$(MAPI_DIR)/include/JETWARE3_7/ \
	#-I$(POWERKEY_DIR)/cak/cak-glue/inc/ \
	#-I$(POWERKEY_DIR)/cak/cak-glue/inc/app \


# CFlags
CFLAGS = \
	-g				 \
	-pipe -fpic -DPIC -std=c99           \
	-DUCLIBC_VERSION=libuClibc-0.9.29.so \
	-W -Wimplicit -Wall                  \
	-O2 -MMD -mips32                     \
	-D_FILE_OFFSET_BITS=64               \
	-D_LARGEFILE_SOURCE                  \
	-D_LARGEFILE64_SOURCE                \
	-D_GNU_SOURCE=1                      \
	-DLINUX                              \
	-DBDBG_DEBUG_BUILD=1                 \
	-DBSTD_CPU_ENDIAN=BSTD_ENDIAN_LITTLE \
	-DPORTING_DEBUG_ON \
#	-DMIDDLEWARE_DEBUG_ON

CFLAGS += $(DEFINES) $(INCLUDEDIRS)
ifeq ($(DVMI_DEBUG),y)
CFLAGS +=-g -rdynamic
endif

# LDFlags
LDFLAGS = -L$(DVMI_DIR)/lib

# AR flags
ARFLAGS = rcs

# LIBS
LIBS = -lstdc++  -lpthread -lm -lrt -lc -ldl -lcrypt
#LIBS += -L$(ROOT_DIR)/dvmi/libs -lmapi -lsettop
LIBS += -L$(ROOT_DIR)/mapi/bin -lmapi -lsettop
LIBS += -L$(BSEAV)/lib/bgfx/build -lgfx
STATIC_LIBS +=  -L$(ROOT_DIR)/dvmi/libs  \
		-ldvmi




# Build rules
##############################################################################
#.SILENT:

#.PHONY: clean build_info

build_info:
	@echo "const static char *build_date = \"`date`\";" > build_info.h

# Build application if specified, link this object with local libs
dvmi: build_info $(OBJS) $(CXXOBJS)
	@echo "Creating application $(TARGETS) ..."
	$(RM) $(TARGETS)*
	$(CC)  $(OBJS) $(STATIC_LIBS) $(LIBS)  $(CXXOBJS) $(LDFLAGS)  -o $(TARGETS)
#	$(ROOT_DIR)/bin/mksquashfs $(ROOT_DIR)/rootfs $(INSTALL_DIR)/app_dev_squashfs.img -root-owned -pf $(ROOT_DIR)/bin/devtable.txt

# Just generate the obj file , for testing
obj: build_info $(OBJS)
	@echo "Creating the OBJ file finished"
	
$(TARGETS):dvmi

install:$(TARGETS)
	cp -f $(TARGETS) $(NFS_ROOT_DIR)
#	$(ROOT_DIR)/bin/mksquashfs $(ROOT_DIR)/rootfs $(INSTALL_DIR)/app_dev_squashfs.img -root-owned -pf $(ROOT_DIR)/bin/devtable.txt



#install: csvt
#	cp -f $(TARGETS)  $(DVMI_DIR)/misc/appfs/bin/

#install:$(TARGETS)
#	$(ROOT_DIR)/bin/mkfs.jffs2 $(ROOT_DIR)/rootfs $(INSTALL_DIR)/app_dev_squashfs.img -root-owned -pf $(ROOT_DIR)/bin/devtable.txt

# Compilation target for C files
$(OBJ_DIR)/%.o:$(SRC_DIR)/%.c 
	@echo "Compiling DVMISRC$< ..."
	$(CC) -c $(CFLAGS) -o $@ $<

clean:
	@echo Cleaning $(TARGETS)
	$(RM) $(OBJS) $(patsubst %.o, %.d, $(OBJS)) $(TARGETS)
	

